<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="用户名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系方式" prop="workerName">
        <el-input
          v-model="queryParams.workerName"
          placeholder="请输入联系方式"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入订单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态">
        <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable size="small" prop="status">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态">
        <el-select v-model="queryParams.cancelStatus" placeholder="选择审核" clearable size="small" prop="cancelStatus">
          <el-option
            v-for="dict in Options"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <!--<el-form-item label="订单状态" prop="orderStatus">-->
      <!--<el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable size="small">-->
      <!--<el-option label="请选择字典生成" value="" />-->
      <!--</el-select>-->
      <!--</el-form-item>-->
      <el-form-item label="时间范围">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>




      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <!--<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

    </el-row>

    <el-table v-loading="loading" :data="orderList"  style="width: 100%">
      <!--      <el-table-column type="selection" width="55" align="center" />-->
      <!--      @selection-change="handleSelectionChange"-->
      <!--<el-table-column label="订单id" align="center" prop="orderId" />-->
      <el-table-column label="订单号" align="center" prop="orderCode"  width="200px">
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            @click="handleOder(scope.row)"-->
<!--          >{{scope.row.orderCode}}</el-button>-->
<!--        </template>-->
      </el-table-column>
      <el-table-column label="用户名" align="center" prop="userName" />
      <el-table-column label="用户联系方式" align="center" prop="telphone"  width="110"/>
      <!--<el-table-column label="订单号" align="center" prop="typeDetailId" />-->
      <!--<el-table-column label="用户id" align="center" prop="mobileUserId" />-->
      <!--<el-table-column label="公司id" align="center" prop="deptId" />-->
      <!--<el-table-column label="工人id" align="center" prop="userId" />-->
      <el-table-column label="订单状态 " align="center" prop="orderStatus":formatter="statusFormat" />

      <el-table-column label="订单总金额" align="center" prop="totalMoney" />
      <el-table-column label="实付金额" align="center" prop="money" />
      <el-table-column label="优惠金额" align="center" prop="couponMoney" />
      <el-table-column label="上门时间" align="center" prop="serviceTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.serviceTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="服务地址" align="center" prop="address"   width="200px">
      <template slot-scope="scope">
        <span>{{scope.row.cityName}}{{ scope.row.address}}</span>
      </template>
      </el-table-column>
      <!--<el-table-column label="支付方式 1-支付宝 2-微信(数据字典)" align="center" prop="payType" />-->
      <!--<el-table-column label="支付时间" align="center" prop="payTime" width="180">-->
        <!--<template slot-scope="scope">-->
          <!--<span>{{ parseTime(scope.row.payTime) }}</span>-->
        <!--</template>-->
      <!--</el-table-column>-->
      <!--<el-table-column label="项目名称" align="center" prop="serviceName" />-->
      <!--<el-table-column label="姓名" align="center" prop="name" />-->
      <!--<el-table-column label="联系方式" align="center" prop="phone" />-->
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.serviceTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="指派公司" align="center" prop="deptName" width="100"/>
      <el-table-column label="服务人员" align="center" prop="workerName" width="110"/>
      <el-table-column label="审核状态" align="center" prop="examineStatus":formatter="statusFormat1" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width"  width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if = "!(scope.row.userId)&& !(scope.row.deptId)&&(scope.row.orderStatus == 1)"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:order:edit']"

          >派单公司</el-button>
          <el-button
            v-if = "!(scope.row.userId)&&!(scope.row.deptId)&&(scope.row.orderStatus == 1)"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handlePerson(scope.row)"
            v-hasPermi="['system:order:edit']"
          >派单个人</el-button>
          <el-button
            v-if = "(scope.row.cancelStatus === 0)"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="sh(scope.row)"
            v-hasPermi="['system:order:edit']"
          >审核</el-button>
          <el-button
            v-if="!(scope.row.userId)&&(scope.row.deptId)&&(scope.row.orderStatus == 1)"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handle(scope.row)"
            v-hasPermi="['system:order:edit']"
          >派单</el-button>
          <!--<el-button-->
            <!--size="mini"-->
            <!--type="text"-->
            <!--icon="el-icon-delete"-->
            <!--@click="handleDelete(scope.row)"-->
            <!--v-hasPermi="['system:order:remove']"-->
          <!--&gt;删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户服务订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" v-show="this.open1">
        <el-form-item label="派单公司" prop="deptId">
          <treeselect v-model="form.deptId" :options="deptOptions" placeholder="请选择公司部门" />
        </el-form-item>
      </el-form>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" v-show="this.open2">
        <el-form-item label="派单个人" >
          <el-select v-model="person"  filterable placeholder="请选择工人"  value-key="userId">
            <el-option
              v-for="item in deptOptions"
              :key="item.userId"
              :label="item.userName"
              :value="item">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" v-show="this.open3">
        <el-form-item label="派单" >
          <el-select v-model="person"  filterable placeholder="请选择"  value-key="userId" >
            <el-option
              v-for="item in deptOptions"
              :key="item.userId"
              :label="item.userName"
              :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <!--<el-form-item label="派单公司" prop="deptId">-->
        <!--<treeselect v-model="form.deptId" :options="deptOptions" placeholder="请选择公司部门" />-->
        <!--</el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog title="订单详情" :visible.sync="orderTableShow" width="600px">
      <el-table v-loading="loading" :data="orderTableList">
        <el-table-column label="用户名" align="center" prop="userName" />
      </el-table>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="open4" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="审核原因" prop="reason">
          <el-input type="textarea"  v-model="form.reason" disabled placeholder="" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="margin-bottom: 30px">
        <el-button type="primary" style="float: left;" @click="submitForm1">审核成功</el-button>
        <el-button style="float: right;"  @click="submitForm2">审核失败</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder,sh,treeselect,personlist,plist, getOrder, delOrder, addOrder, updateOrder, exportOrder } from "@/api/system/order";

  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  export default {
    components: { Treeselect },
    name: "Order",
    data() {
      return {
        //点击订单号展示的表格DIALOG
        orderTableList:[],
        orderTableShow:false,
        // 遮罩层
        defaultProps: {
          children: "children",
          label: "label"
        },

      loading: true,
      // 选中数组
      ids: [],
      Options:[],
      deptOptions: undefined,
      statusOptions:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      person:[],
      // 用户服务订单表格数据
      orderList: [],
      dateRange: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open4:false,
      open: false,
      open1:false,
      open2:false,
      open3:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderCode: undefined,
        typeDetailId: undefined,
        mobileUserId: undefined,
        deptId: undefined,
        userId: undefined,
        orderStatus: undefined,
        payType: undefined,
        payTime: undefined,
        serviceName: undefined,
        name: undefined,
        phone: undefined,
        serviceTime: undefined,
        serviceDetail: undefined,
        addressId: undefined,
        cityId: undefined,
        orderType: undefined,
        money: undefined,
        totalMoney: undefined,
        tailMoney: undefined,
        skuId: undefined,
        data: undefined,
        couponId: undefined,
        couponMoney: undefined,
        total: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_order_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("sys_audit_status").then(response => {
      this.Options = response.data;
    });
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  methods: {
    //获取订单表格
    handleOder(row){
      console.log('订单表格',row)
      var goodsOrderId = row.goodsOrderId
      this.orderTableShow = true
    },
    /** 查询用户服务订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.orderStatus);
    },
    statusFormat1(row, column) {
      return this.selectDictLabel(this.Options , row.cancelStatus);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.person = undefined,
      this.form = {
        cancelStatus:undefined,
        orderId: undefined,
        orderCode: undefined,
        typeDetailId: undefined,
        mobileUserId: undefined,
        deptId: undefined,
        userId: undefined,
        orderStatus: undefined,
        payType: undefined,
        payTime: undefined,
        serviceName: undefined,
        name: undefined,
        phone: undefined,
        serviceTime: undefined,
        serviceDetail: undefined,
        addressId: undefined,
        cityId: undefined,
        orderType: undefined,
        money: undefined,
        totalMoney: undefined,
        tailMoney: undefined,
        createTime: undefined,
        remark: undefined,
        skuId: undefined,
        data: undefined,
        couponId: undefined,
        couponMoney: undefined,
        total: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户服务订单";
    },

    /** 派单服务 */
    handleUpdate(row) {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
      this.reset();
      this.open1 = true;
      this.open2 = false;
      this.open3 = false;
      console.log(row.userId)
      // const orderId = row.orderId || this
      this.form.orderId = row.orderId
      this.form.userId = row.userId
        this.open = true;
        this.title = "派单公司";

    },
    sh(row){

      this.open1 = false ;
      this.open2 = false;
      this.open3 = false;
      this.open4 = true;
      this.reset();
      this.form.reason = row.reason
      this.form.orderId = row.orderId
    },
    handlePerson(row){
      this.open1 = false ;
      this.open2 = true;
      this.open3 = false;
      personlist().then(response => {
        this.deptOptions = response.data;

      });
      this.reset();
      this.form.orderStatus = 2;
      this.form.orderId = row.orderId
      this.open = true;
      this.title = "派单个人";
    },
    handle(row){
      this.open1 = false ;
      this.open2 = false;
      this.open3 = true;
      plist().then(response => {
        this.deptOptions = response.data;
        console.log(this.deptOptions)
      });
      this.reset();
      this.form.orderStatus = 2;
      this.form.orderId = row.orderId
      this.open = true;
      this.title = "派单服务";
    },
    /** 提交按钮 */
    submitForm: function() {
      console.log(this.person)
      if(this.person) {
        this.form.deptId =this.person.deptId
        this.form.userId =this.person.userId
      }

      this.$refs["form"].validate(valid => {
        if (valid) {
          updateOrder(this.form).then(response => {

              if (response.code === 200) {
                this.msgSuccess("派单成功");
                this.open = false;
                this.getList();
                this.reset();
              } else {
                this.msgError(response.msg);

              }
            });

        }
      });
    },

    submitForm1: function() {
      this.form.cancelStatus = 1;
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orderId != undefined) {
            sh(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("审核成功");
                this.open4 = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            sh(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open4 = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    submitForm2: function() {
      this.form.cancelStatus = 2;
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orderId!= undefined) {
           sh(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open4 = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            sh(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open4 = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    // handleDelete(row) {
    //   const orderIds = row.orderId || this.ids;
    //   this.$confirm('是否确认删除用户服务订单编号为"' + orderIds + '"的数据项?', "警告", {
    //       confirmButtonText: "确定",
    //       cancelButtonText: "取消",
    //       type: "warning"
    //     }).then(function() {
    //       return delOrder(orderIds);
    //     }).then(() => {
    //       this.getList();
    //       this.msgSuccess("删除成功");
    //     }).catch(function() {});
    // },
    /** 导出按钮操作 */
    // handleExport() {
    //   const queryParams = this.queryParams;
    //   this.$confirm('是否确认导出所有用户服务订单数据项?', "警告", {
    //       confirmButtonText: "确定",
    //       cancelButtonText: "取消",
    //       type: "warning"
    //     }).then(function() {
    //       return exportOrder(queryParams);
    //     }).then(response => {
    //       this.download(response.msg);
    //     }).catch(function() {});
    // }
  }
};
</script>
